<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="data" default-expand-all>
          <template v-slot="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <addor-edit v-if="showDialog" :pid="curId" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="hClose" />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { arrayToTree } from '@/utils/index'
import AddorEdit from './deptDialog.vue'
import { delDepartment } from '@/api/departments'
export default {
  components: { AddorEdit },

  data() {
    return {
      isEdit: false,
      curId: '',
      showDialog: false,
      data: [],
      // 收集数据提供给子组件做表单校验时用
      originList: []
    }
  },
  created() {
    // 发请求
    this.loadDepartments()
  },
  methods: {
    hClose() {
      this.showDialog = false
    },
    hAdd(id) {
      this.showDialog = true
      console.log(id)
      this.curId = id
      this.isEdit = false
    },
    hEdit(data) {
      this.showDialog = true
      this.curId = data.id
      this.isEdit = true
    },
    async doDel(id) {
      await delDepartment(id)
      this.$message.success('删除成功!')
      this.loadDepartments()
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res)
        // 由于业务中传智教育的pid是:-1，它是最高级的节点
        // shift 从数组头部取出一个元素
        const root = res.data.depts.shift()
        console.log(root)
        // 收集数据提供给子组件做表单校验时用
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            pid: item.pid,
            code: item.code,
            name: item.name }
        })
        // 用工具函数把 平铺的数组转成 树型数据
        this.data = arrayToTree(res.data.depts)
      } catch (err) {
        console.log(err)
      }
    },
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartments()
    }
  }
}
</script>

<style></style>
